package com.xiaoc.bulldozer.controller;


import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.SendMessageConstat;
import com.xiaoc.bulldozer.facade.SendMessageFacade;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;

@Controller
@RequestMapping(value = "/sendMessage")
public class SendMessageController extends AdminBaseController {

    private Logger logger = LoggerFactory.getLogger(SendMessageController.class);

    @Resource(name = "sendMessageFacade")
    private SendMessageFacade sendMessageFacade;

    @GetMapping(value = "/list")
    public ModelAndView list() {

        return renderHtml("sendMessage/list.html", null);
    }


    @PostMapping(value = "/send")
    public ModelAndView send(MultipartFile file, HttpServletRequest request) {

        String type = request.getParameter("type");
        if (StringUtils.isBlank(type)) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "请选择消息类型！");
        }


        String message1 = request.getParameter("message1");
        String message2 = request.getParameter("message2");

        if (StringUtils.isBlank(message1)) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "请填写消息内容！");
        }


        Set<String> phoneNumberSet = new TreeSet<>();

        if (StringUtils.equals(type, SendMessageConstat.WEIXIN_MESSAGE) || StringUtils.equals(type, SendMessageConstat.SMS)) {

            if (file == null || StringUtils.isBlank(file.getOriginalFilename())) {

                return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "请上传文件！");
            }

            phoneNumberSet = readFile(file);
            if (CollectionUtils.isEmpty(phoneNumberSet)) {
                return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "上传文件没有内容！");
            }

        }


        //微信模板消息
        if (type.equals(SendMessageConstat.WEIXIN_MESSAGE)) {

            if (StringUtils.isBlank(message2)) {
                return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "请填写消息内容！");
            }

            sendMessageFacade.sendWeixinMessage(phoneNumberSet, message1, message2);

        } else if (type.equals(SendMessageConstat.SMS)) {

            sendMessageFacade.sendSMS(phoneNumberSet, message1);

        } else {
            sendMessageFacade.sendSMSByType(type, message1);
        }


        return jsonSucess();
    }

    private Set<String> readFile(MultipartFile file) {

        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        Set<String> phoneNumberSet = new TreeSet<>();

        try {

            inputStreamReader = new InputStreamReader(file.getInputStream(), "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);

            String phoneNumber;

            while ((phoneNumber = bufferedReader.readLine()) != null) {
                phoneNumberSet.add(StringUtils.trim(phoneNumber));
            }

        } catch (Exception e) {
            logger.error("readFile error:{}", e);
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
            } catch (IOException e) {
                logger.error("readFile error:{}", e);
                e.printStackTrace();
            }
            return phoneNumberSet;
        }
    }


}
